/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "reader.h"

#include <gtest/gtest.h>
#include <memory>

#include "ipc_object_stub.h"
#include "loghelper.h"
#include "se_service.h"
#include "session.h"
#include "service_manager.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace OHOS::SE::OMAPI;
const std::string ESE_READER{"eSE1"};
static bool isSeSupported = false;

class ReaderTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
    void SetUp();
    void TearDown();
public:
    std::shared_ptr<Reader> reader_;
    std::shared_ptr<SEService> seService_;
};

void ReaderTest::SetUp()
{
    InfoLog("ReaderTest::SetUp Begin!");
    seService_ = std::make_shared<SEService>();
    bool initStatus = false;
    seService_->InitSEService(initStatus, nullptr);
    sleep(1);
    EXPECT_TRUE(initStatus);
    std::vector<std::shared_ptr<Reader>> readerLists;
    seService_->GetReaders(readerLists);
    isSeSupported = readerLists.empty();
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    for (auto reader : readerLists) {
        std::string readerName {};
        reader->GetName(readerName);
        if (readerName.compare("eSE") == 0) {
            reader_ = reader;
            break;
        }
    }
    EXPECT_TRUE(reader_);
    InfoLog("ReaderTest::SetUp done!");
}

void ReaderTest::TearDown()
{
    InfoLog("ReaderTest::TearDown Begin!");
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (reader_) {
        reader_->CloseSessions();
        reader_ = nullptr;
    }
    if (seService_) {
        seService_->Shutdown();
        seService_ = nullptr;
    }
    InfoLog("ReaderTest::TearDown done!");
}

/**
 * @tc.name: GetName_Test001
 * @tc.desc: Test Reader GetName.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, GetName_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (reader_ != nullptr) {
        std::string name;
        int statusCode = reader_->GetName(name);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(name.length() > 0);
    }
}

/**
 * @tc.name: IsSecureElementPresent_Test001
 * @tc.desc: Test Reader IsSecureElementPresent.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, IsSecureElementPresent_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (reader_ != nullptr) {
        bool isSePresent = false;
        int statusCode = reader_->IsSecureElementPresent(isSePresent);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(isSePresent);
    }
}

/**
 * @tc.name: GetSEService_Test001
 * @tc.desc: Test Reader GetSEService.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, GetSEService_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (reader_ != nullptr) {
        std::weak_ptr<SEService> seService;
        int statusCode = reader_->GetSEService(seService);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(seService.expired());
    }
}

/**
 * @tc.name: OpenSession_Test001
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, OpenSession_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (reader_ != nullptr) {
        std::shared_ptr<Session> session;
        int statusCode = reader_->OpenSession(session);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(session);

        if (session != nullptr) {
            bool isClosed = false;
            statusCode = session->IsClosed(isClosed);
            EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
            EXPECT_FALSE(isClosed);

            statusCode = reader_->CloseSessions();
            EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
            statusCode = session->IsClosed(isClosed);
            EXPECT_TRUE(isClosed);
        }
    }
}

/**
 * @tc.name: Reset_Test001
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, Reset_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (reader_ != nullptr) {
        bool resetStatus = false;
        int statusCode = reader_->Reset(resetStatus);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(resetStatus);
    }
}

/**
 * @tc.name: InnerCheckReaderStatus_Test001
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, InnerCheckReaderStatus_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementReader> readerIface = nullptr;
    std::string readerName = "eSE";
    std::shared_ptr<Reader> reader = std::make_shared<Reader>(service, readerName, readerIface);
    if (reader != nullptr) {
        std::shared_ptr<Session> session;
        int statusCode = reader->OpenSession(session);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: IsSecureElementPresent_Test002
 * @tc.desc: Test Reader IsSecureElementPresent.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, IsSecureElementPresent_Test002, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::string readerName = "eSE";
    sptr<ISecureElementReader> readerIface = nullptr;
    std::shared_ptr<Reader> reader = std::make_shared<Reader>(service, readerName, readerIface);
    if (reader != nullptr) {
        bool isPresent = false;
        int statusCode = reader->IsSecureElementPresent(isPresent);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: Reset_Test002
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, Reset_Test002, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::string readerName = "eSE";
    sptr<ISecureElementReader> readerIface = nullptr;
    std::shared_ptr<Reader> reader = std::make_shared<Reader>(service, readerName, readerIface);
    if (reader != nullptr) {
        bool resetStatus = false;
        int statusCode = reader->Reset(resetStatus);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: CloseSessions_Test001
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(ReaderTest, CloseSessions_Test001, TestSize.Level1)
{
    if (isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::string readerName = "eSE";
    sptr<ISecureElementReader> readerIface = nullptr;
    std::shared_ptr<Reader> reader = std::make_shared<Reader>(service, readerName, readerIface);
    if (reader != nullptr) {
        int statusCode = reader->CloseSessions();
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
} // TEST
} // SE
} // OHOS